package com.ysyx.ysyx_passengerorderquery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysyx.ysyx_passengerorderquery.dto.BaseDto;
import com.ysyx.ysyx_passengerorderquery.pojo.OrderPojo;
import com.ysyx.ysyx_passengerorderquery.qo.TakeOrderQo;
import com.ysyx.ysyx_passengerorderquery.service.OrderPojoService;
import com.ysyx.ysyx_passengerorderquery.mapper.OrderPojoMapper;
import org.redisson.api.RGeo;
import org.redisson.api.RMap;
import org.redisson.api.RSet;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
* @author 薛宇晖
* @description 针对表【order_inf(订单表)】的数据库操作Service实现
* @createDate 2023-04-17 15:07:58
*/
@Service
public class OrderPojoServiceImpl
    implements OrderPojoService{

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private OrderPojoMapper orderPojoMapper;

    /*
     * @Description: (作用)根据订单id查看订单详情
     * @params: orderId
     * @return:
     * @Author: 薛宇晖
     * @create:2023/4/17
     */
    @Override
    public BaseDto queryOrderinfo(String orderId) throws ParseException, InterruptedException {

        List<TakeOrderQo> qos = new ArrayList<>();

        RMap<Object, Object> map = redissonClient.getMap("orderinfo:" + orderId);
//        for (Map.Entry<Object,Object> entry:map.entrySet()){
//            System.out.println("K="+entry.getKey()+"--V="+entry.getValue());
//        }


        TakeOrderQo o = new TakeOrderQo();
        o.setOrderId((String) map.get("orderId"));
        System.out.println("进入此");
        System.out.println(o);
        o.setPassengerId(Integer.valueOf((String) map.get("passengerId")));
        o.setOrderStatus(Integer.valueOf((String) map.get("orderStatus")));
        o.setOrderCost((Double.parseDouble((String) map.get("orderCost"))));
        o.setOrderMileage((Double.parseDouble((String) map.get("orderMileage"))) );
        if (map.get("driverId")!=null){
            o.setDriverId((Integer.valueOf((String) map.get("driverId"))));


            RMap<Object, Object> map1 = redissonClient.getMap("driverinfo:" + o.getDriverId());
            o.setDriverTel((String) map.get("driverTel"));


            o.setDriverName((String) map1.get("driverName"));
            o.setCarBrand((String) map1.get("carBrand"));
            o.setCarColor((String) map1.get("carColor"));
            o.setCarPlateno((String) map1.get("carPlateno"));

            RGeo<String> geo = redissonClient.getGeo("order:track:" + orderId);
//            for (String g : geo) {
//
//                double longitude = geo.pos(g).get(g).getLongitude();
//                double latitude = geo.pos(g).get(g).getLatitude();
//
//
//            }
            Object[] objects = geo.toArray();
            if (objects.length==0) return new BaseDto(101,"没有订单数据",null);

            String s  = (String) objects[objects.length-1];
            double longitude =geo.pos(s).get(s).getLongitude();
            double latitude = geo.pos(s).get(s).getLatitude();
            o.setDriverLongitude(longitude);
            o.setDriverLatitude(latitude);



        }

        o.setStartName((String) map.get("startName"));
        o.setEndName((String) map.get("endName"));
        o.setStartLatitude(Double.parseDouble((String) map.get("startLatitude")) );
        o.setStartLongitude(Double.parseDouble((String) map.get("startLongitude")) );
        o.setEndLatitude(Double.parseDouble((String) map.get("endLatitude")) );
        o.setEndLongitude(Double.parseDouble((String) map.get("endLongitude")) );
        o.setPassengerTel((String) map.get("passengerTel"));
//            (Date) orderMap.get("orderStatustime")
//            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-03 10:59:27")

        o.setOrderStatustime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
        System.err.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
        o.setOrderStatus((Integer.valueOf((String) map.get("orderStatus"))));
        //获取到对象

        qos.add(o);
        if (qos.size()==0){
            //没有获取到
//            去查数据库
            System.out.println("差的数据库");
            TakeOrderQo takeOrderQo = new TakeOrderQo();
            takeOrderQo.setOrderId(orderId);
            TakeOrderQo takeOrderQo1 = orderPojoMapper.queryOrderinfoByorderid(takeOrderQo);
            if (takeOrderQo1==null){
                return new BaseDto(101,"订单数据丢失",null);
            }
            qos.add(takeOrderQo1);

        }

        return new BaseDto(201,"获取到订单详细信息",qos);
    }

    /*
     * @Description: (作用)乘客查看有没有未完成/未支付的订单
     * @params: passengerId 乘客id
     * @return:
     * @Author: 薛宇晖
     * @create:2023/4/18
     */
    @Override
    public BaseDto queryDoingOrderBypassenger(int passengerId) {
//        redissonClient.getMap()
        System.err.println("xxx");

        RSet<Object> set = redissonClient.getSet("tense:order");
        List<TakeOrderQo> takeOrderQos = new ArrayList<>();
        System.out.println(set.size());
        for (Object o : set) {
            //正在行程中的订单的set池
            RMap<Object, Object> map = redissonClient.getMap("orderinfo:" + o);
            Integer passengerId1 = Integer.valueOf(String.valueOf(map.get("passengerId")));
            System.err.println(passengerId1);
            if (passengerId1!=passengerId){
                continue;
            }


            int orderStatus =Integer.valueOf(String.valueOf(map.get("orderStatus")));
            System.err.println(orderStatus);
            if (orderStatus==301||orderStatus==401||orderStatus==402
            ||orderStatus==403||orderStatus==505||orderStatus==506
            ||orderStatus==507){
                //行程中的订单状况
                TakeOrderQo takeOrderQo = new TakeOrderQo();
                takeOrderQo.setOrderId((String) map.get("orderId"));
                System.out.println("进入此");
                System.out.println(takeOrderQo);
                takeOrderQo.setPassengerId(Integer.valueOf((String) map.get("passengerId")));
                takeOrderQo.setOrderStatus(Integer.valueOf((String) map.get("orderStatus")));
                takeOrderQo.setOrderCost((Double.parseDouble((String) map.get("orderCost"))));
                takeOrderQo.setOrderMileage((Double.parseDouble((String) map.get("orderMileage"))) );
                if (map.get("driverId")!=null){
                    takeOrderQo.setDriverId((Integer.valueOf((String) map.get("driverId"))));
                    takeOrderQo.setDriverTel((String) map.get("driverTel"));
                    takeOrderQo.setDriverName((String) map.get("driverName"));
                    RMap<Object, Object> map1 = redissonClient.getMap("driverinfo:" + takeOrderQo.getDriverId());

                    takeOrderQo.setCarBrand((String) map1.get("carBrand"));
                    takeOrderQo.setCarColor((String) map1.get("carColor"));
                    takeOrderQo.setCarPlateno((String) map1.get("carPlateno"));
                    RGeo<String> geo = redissonClient.getGeo("order:track:" + o);
//                    for (String g : geo) {
//
//                        double longitude = geo.pos(g).get(g).getLongitude();
//                        double latitude = geo.pos(g).get(g).getLatitude();
//                        takeOrderQo.setDriverLongitude(longitude);
//                        takeOrderQo.setDriverLatitude(latitude);
//                    }
                    Object[] objects = geo.toArray();
                    if (objects.length==0) return new BaseDto(444,"没有订单数据",null);
                    String g  = (String) objects[objects.length-1];
                    double longitude = geo.pos(g).get(g).getLongitude();
                    double latitude = geo.pos(g).get(g).getLatitude();
                    takeOrderQo.setDriverLongitude(longitude);
                    takeOrderQo.setDriverLatitude(latitude);


                }

                takeOrderQo.setStartName((String) map.get("startName"));
                takeOrderQo.setEndName((String) map.get("endName"));
                takeOrderQo.setStartLatitude(Double.parseDouble((String) map.get("startLatitude")) );
                takeOrderQo.setStartLongitude(Double.parseDouble((String) map.get("startLongitude")) );
                takeOrderQo.setEndLatitude(Double.parseDouble((String) map.get("endLatitude")) );
                takeOrderQo.setEndLongitude(Double.parseDouble((String) map.get("endLongitude")) );
                takeOrderQo.setPassengerTel((String) map.get("passengerTel"));
//            (Date) orderMap.get("orderStatustime")
//            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-03 10:59:27")

                try {
                    takeOrderQo.setOrderStatustime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
                    System.err.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
                    System.out.println("标记");
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                takeOrderQo.setOrderStatus((Integer.valueOf((String) map.get("orderStatus"))));
                takeOrderQos.add(takeOrderQo);
                return new BaseDto(101,"查询到行程中的订单",takeOrderQos);

            }else if (orderStatus==601||orderStatus==602){
                //行程中的订单状况
                TakeOrderQo takeOrderQo = new TakeOrderQo();
                takeOrderQo.setOrderId((String) map.get("orderId"));
                System.out.println("进入此");
                System.out.println(takeOrderQo);
                takeOrderQo.setPassengerId(Integer.valueOf((String) map.get("passengerId")));
                takeOrderQo.setOrderStatus(Integer.valueOf((String) map.get("orderStatus")));
                takeOrderQo.setOrderCost((Double.parseDouble((String) map.get("orderCost"))));
                takeOrderQo.setOrderMileage((Double.parseDouble((String) map.get("orderMileage"))) );
                if (map.get("driverId")!=null){
                    takeOrderQo.setDriverId((Integer.valueOf((String) map.get("driverId"))));
                    takeOrderQo.setDriverTel((String) map.get("driverTel"));

                    RMap<Object, Object> map1 = redissonClient.getMap("driverinfo:" + takeOrderQo.getDriverId());
                    takeOrderQo.setDriverName((String) map1.get("driverName"));
                    takeOrderQo.setCarBrand((String) map1.get("carBrand"));
                    takeOrderQo.setCarColor((String) map1.get("carColor"));
                    takeOrderQo.setCarPlateno((String) map1.get("carPlateno"));
                    RGeo<String> geo = redissonClient.getGeo("order:track:" + o);
//                    for (String g : geo) {
//
//                        double longitude = geo.pos(g).get(g).getLongitude();
//                        double latitude = geo.pos(g).get(g).getLatitude();
//                        takeOrderQo.setDriverLongitude(longitude);
//                        takeOrderQo.setDriverLatitude(latitude);
//                    }
                    Object[] objects = geo.toArray();
                    String s  = (String) objects[objects.length-1];
                    double longitude =geo.pos(s).get(s).getLongitude();
                    double latitude = geo.pos(s).get(s).getLatitude();
                    takeOrderQo.setDriverLongitude(longitude);
                    takeOrderQo.setDriverLatitude(latitude);


                }

                takeOrderQo.setStartName((String) map.get("startName"));
                takeOrderQo.setEndName((String) map.get("endName"));
                takeOrderQo.setStartLatitude(Double.parseDouble((String) map.get("startLatitude")) );
                takeOrderQo.setStartLongitude(Double.parseDouble((String) map.get("startLongitude")) );
                takeOrderQo.setEndLatitude(Double.parseDouble((String) map.get("endLatitude")) );
                takeOrderQo.setEndLongitude(Double.parseDouble((String) map.get("endLongitude")) );
                takeOrderQo.setPassengerTel((String) map.get("passengerTel"));
//            (Date) orderMap.get("orderStatustime")
//            new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2019-01-03 10:59:27")

                try {
                    takeOrderQo.setOrderStatustime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
                    System.err.println(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse((String)map.get("orderStatustime")));
                } catch (ParseException e) {
                    throw new RuntimeException(e);
                }
                takeOrderQo.setOrderStatus((Integer.valueOf((String) map.get("orderStatus"))));
                takeOrderQos.add(takeOrderQo);
                return new BaseDto(102,"存在未支付订单",takeOrderQos);

            }




        }

        if (takeOrderQos.size()==0){
            //takeOrder redis如果差不到 我才去查数据库
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.select().orderByDesc("order_statustime");
            queryWrapper.eq("passenger_id",passengerId);
            List<OrderPojo> list = orderPojoMapper.selectList(queryWrapper);
            if (list.size()==0){
                //查不到
                return new BaseDto(301,"没有下过订单该乘客",null);
            }
            OrderPojo orderPojo = list.get(0);
            TakeOrderQo takeOrderQo = new TakeOrderQo();
            takeOrderQo.setOrderId(orderPojo.getOrderId());
            System.out.println(orderPojo.getOrderId());
            TakeOrderQo takeOrderQo1 = orderPojoMapper.queryOrderinfoByorderid(takeOrderQo);
            System.out.println(takeOrderQo1.toString());
            takeOrderQos.add(takeOrderQo1);


            return new BaseDto(102,"请杰荣自己看状态码",takeOrderQos);



        }


        return new BaseDto(444,"应该不会走到这吧，走到这就是没有",null);
    }
}




